import {
  Inject,
  Controller,
  Get,
  Post,
  Body,
  File,
  Fields
} from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { UserService } from '../service/user.service';
import { UploadMiddleware, UploadFileInfo } from '@midwayjs/busboy';

@Controller('/auth-api')
export class UserController {
  @Inject()
  ctx: Context;

  @Inject()
  userService: UserService;

  @Get('/current')
  async current() {
    return await this.userService.current();
  }

  @Post('/logout')
  async logout() {
    return await this.userService.logout();
  }

  @Post('/sync-wx-info')
  async syncWxInfo(
    @Body() body: { target: string; iv: string; login_code: string }
  ) {
    const { target, iv, login_code } = body;
    return await this.userService.decodeWxEncryptedData(target, iv, login_code);
  }

  @Post('/wx/user/update', { middleware: [UploadMiddleware] })
  async updateUserInfo(
    @File()
    file: UploadFileInfo,
    @Fields('user_nickname')
    user_nickname: string
  ) {
    return this.userService.wxUpdateUserInfo(file, user_nickname);
  }

  @Post('/wx/user/update/nickname')
  async updateUserNickname(
    @Body('user_nickname')
    user_nickname: string
  ) {
    return this.userService.wxUpdateUserInfo(null as any, user_nickname);
  }
}
